<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		width="1100px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4 flex-end-center">
			<jk-search-input v-model="searchParams.productNameOrCode" placeholder="品种" class="search-bar-item-size3 item-gap"></jk-search-input>
			<jk-search-input v-model="searchParams.batchCode" placeholder="批号" class="search-bar-item-size2 item-gap"></jk-search-input>
			<jk-select v-model="searchParams.processId" :option-list="processList" placeholder="工序" class="search-bar-item-size1 item-gap"></jk-select>
			<jk-select v-model="searchParams.spinTypeId" :option-list="spinTypeList" placeholder="纺纱方式" clearable class="search-bar-item-size1 item-gap"></jk-select>
			<jk-search-input v-model="searchParams.machineModelName" placeholder="机型" class="search-bar-item-size2 item-gap"></jk-search-input>
			<jk-search-input v-model="searchParams.code" placeholder="用户工艺编号" class="search-bar-item-size2 item-gap"></jk-search-input>
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="dialogTableHeight - 20"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<!--<vxe-column title="工艺单号" align="left" field="code" :width="150">
                        <template slot-scope="scope">
                            <span class="link-style" @click="onDetailBtn(scope)">{{ scope.row.code }}</span>
                        </template>
                    </vxe-column>-->
			<vxe-column title="用户工艺编号" align="left" field="code" :width="150"></vxe-column>
			<vxe-column title="品种" align="left" field="productName" :min-width="140"></vxe-column>
			<vxe-column title="批号" align="left" field="batchCode" :min-width="140"></vxe-column>
			<vxe-column title="纺纱方式" align="left" field="spinTypeName" :width="90"></vxe-column>
			<vxe-column title="工序" align="left" field="processName" :width="60"></vxe-column>
			<vxe-column title="机型" align="left" field="machineModelName" :width="100"></vxe-column>
			<vxe-column title="定量(g/m)" align="left" field="unitPrice" :width="110">
				<template slot-scope="scope">
					<span>{{ scope.row.gramWeight ? `${scope.row.gramWeight}/${scope.row.meters}` : '无' }}</span>
				</template>
			</vxe-column>
			<vxe-column title="工艺员" align="left" field="specEmpName" :width="90"></vxe-column>
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../../mixins/cal-dialog-height';
    import { pageConfig } from '../../../../../utils/jk/jk-tool';
    import { spinSpecSheetPage } from '../../../../../api/technology/order';
    import { systemBusinessDictPage } from '../../../../../api/basicData/businessDict';

    export default {
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            formData: {
                type: Object,
                default() {
                    return {};
                }
            },
            title: {
                type: String,
                default: '品种工艺单'
            }
        },
        data() {
            return {
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    productNameOrCode: '',
                    batchCode: '',
                    categoryId: null,
                    processId: null,
                    selectItem: true,
                    spinTypeId: null,
                    code: ''
                },
                selectRow: null,
                searchBarProcessId: null,
                searchBarNameOrCode: '',
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: [],
                processList: [],
                spinTypeList: []
            };
        },
        methods: {
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectData).length) {
                    this.$emit('on-confirm', this.selectData);
                } else {
                    this.$tipsBar(this, 'unChoose');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                this.selectData = [];
                return spinSpecSheetPage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.tableData = res.data.res;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getSpinTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'SPIN_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.spinTypeList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getSpinTypeListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.processList = this.$store.state.user.userProcess.processList;
                    // 工艺设定，如果有销售工艺，根据销售工艺查询，没有则根据品种、批号、工序、机型、纺纱纺纱
                    if (this.formData.code) {
                        this.$set(this.searchParams, 'code', this.formData.code || '');
                    } else {
                        this.$set(this.searchParams, 'productNameOrCode', this.formData.productName);
                        this.$set(this.searchParams, 'batchCode', this.formData.batchCode);
                        this.$set(this.searchParams, 'processId', this.formData.processId);
                        this.$set(this.searchParams, 'machineModelName', this.formData.machineModelName);
                        this.$set(this.searchParams, 'spinTypeId', this.formData.spinTypeId);
                    }
                    this.getDependentData();
                }
            }
        }
    };
</script>
